package ro.msg.quiz.repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import ro.msg.quiz.dao.QuestionDB;
import ro.msg.quiz.dao.QuizDB;
import ro.msg.quiz.domain.Question;
import ro.msg.quiz.domain.Quiz;

//AUTHOR: Marco

//Singleton class responsible for holding data from database/text files in memory
public class Repository {
    // the first and single instantiation of this class
    private static Repository instance = new Repository();
    // list of quizzes
    private List<Quiz> quizzesList;
    // list of questions
    private List<Question> questionsList;

    // object that deals with populating the lists above
    // private Populater populater;

    // add QuizzesDAO
    // add QuestionDAO

    private Repository() {
	// we had to catch the possible exceptions here because this class is a
	// singleton
	/*
	 * try { this.populater = new Populater(); } catch (Exception e) {
	 * JOptionPane.showMessageDialog(null, "Error reading files!\n" +
	 * e.toString()); }
	 */
	this.quizzesList = new ArrayList<Quiz>();
	this.questionsList = Collections.synchronizedList(new ArrayList<Question>());
    }

    // returns an object of type Repository
    public static Repository getInstance() {
	return instance;
    }

    /**
     * @return the quizzesList
     * @throws Exception
     */
    public List<Quiz> getQuizzesList() {
	/*
	 * Map<Integer, Quiz> quizMap = new HashMap<Integer, Quiz>(); quizMap =
	 * populater.getQuizMap();
	 */

	if (quizzesList.size() == 0) {
	    /*
	     * for (int id : quizMap.keySet()) if (!this.duplicate(id))
	     * this.quizzesList.add(quizMap.get(id));
	     */
	    try {
		quizzesList = QuizDB.populateQuizzes();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    return quizzesList;
	}
	return quizzesList;
    }

    /**
     * @return the questionsList
     * @throws Exception
     */
    public List<Question> getQuestionsList() {
	// Map<Integer, Quiz> quizMap = new HashMap<Integer, Quiz>();
	// quizMap = populater.getQuizMap();

	if (questionsList.size() == 0) {
	    /*
	     * for (int id : quizMap.keySet()) for (Question question :
	     * quizMap.get(id).getQuestionList()) if
	     * (!this.duplicate(question.getText()))
	     * this.questionsList.add(question);
	     */
	    try {
		questionsList = QuestionDB.populateQuestions();
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    return questionsList;
	}
	return questionsList;
    }

    // checks if a text question is contained in questionsList
    // returns false if the text question is unique
    @SuppressWarnings("unused")
    private boolean duplicate(String text) {
	for (Question question : questionsList)
	    if (question.getText().equals(text))
		return true;
	return false;
    }

    // checks if a quiz id is contained in quizzesList
    // returns false if the id is unique
    @SuppressWarnings("unused")
    private boolean duplicate(int id) {
	for (Quiz quiz : quizzesList)
	    if (quiz.getId() == id)
		return true;
	return false;
    }

    /**
     * @param ql
     *            the questionsList to set
     */
    public void setQuestionList(List<Question> ql) {
	questionsList = ql;
    }

    /**
     * @param ql
     *            the quizzesList to set
     */
    public void setQuizzesList(List<Quiz> ql) {
	quizzesList = ql;
    }

}